﻿using System;
using StoCombatLogHUD.Parsing;

namespace StoCombatLogHUD.Statistics
{
	/// <summary>
	/// Usefull extensions for PlayerStatistics.
	/// </summary>
	public static class PlayerStatisticsExtensions
	{
		/// <summary>
		/// Updates statistics from a given combat log entry.
		/// </summary>
		/// <param name="playerStatistics">The player statistics.</param>
		/// <param name="combatLogEntry">The combat log entry.</param>
		/// <exception cref="System.NotSupportedException"></exception>
		public static void UpdateFromCombatLogEntry(this PlayerStatistics playerStatistics, CombatLogEntry combatLogEntry)
		{
			var combatLogEntryType = combatLogEntry.GetCombatLogEntryType();

			if (combatLogEntryType == CombatLogEntryExtensions.CombatLogEntryType.ShieldHealing)
			{
				playerStatistics.TotalHealing.AddCombatLogEntry(combatLogEntry);
				playerStatistics.ShieldHealing.AddCombatLogEntry(combatLogEntry);
				return;
			}

			if (combatLogEntryType == CombatLogEntryExtensions.CombatLogEntryType.HullHealing)
			{
				playerStatistics.TotalHealing.AddCombatLogEntry(combatLogEntry);
				playerStatistics.HullHealing.AddCombatLogEntry(combatLogEntry);
				return;
			}

			if (combatLogEntryType == CombatLogEntryExtensions.CombatLogEntryType.ShieldDamage)
			{
				playerStatistics.TotalDamage.AddCombatLogEntry(combatLogEntry);
				if (combatLogEntry.IsPetEntry())
					playerStatistics.PetDamage.AddCombatLogEntry(combatLogEntry);
				else
					playerStatistics.DamageToShields.AddCombatLogEntry(combatLogEntry);

				playerStatistics.ConditionallyUpdatePeakDamageStatistics(combatLogEntry);
				return;
			}

			if (combatLogEntryType == CombatLogEntryExtensions.CombatLogEntryType.HullDamage)
			{
				playerStatistics.TotalDamage.AddCombatLogEntry(combatLogEntry);
				if (combatLogEntry.IsPetEntry())
					playerStatistics.PetDamage.AddCombatLogEntry(combatLogEntry);
				else
					playerStatistics.DamageToHull.AddCombatLogEntry(combatLogEntry);

				playerStatistics.ConditionallyUpdatePeakDamageStatistics(combatLogEntry);
				return;
			}

// ReSharper disable RedundantJumpStatement
			return;
// ReSharper restore RedundantJumpStatement
		}

		/// <summary>
		/// Conditionally updates the peak damage statistics.
		/// </summary>
		/// <param name="playerStatistics">The player statistics.</param>
		/// <param name="combatLogEntry">The combat log entry.</param>
		private static void ConditionallyUpdatePeakDamageStatistics(this PlayerStatistics playerStatistics, CombatLogEntry combatLogEntry)
		{
			var currentDmg = combatLogEntry.GetEffectiveValue();
			if (currentDmg > playerStatistics.PeakDamage.TotalValue)
				playerStatistics.PeakDamage.TotalValue = currentDmg;
		}
	}
}
